package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.vo.SpuVo;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SpuDescMapper;
import com.atguigu.gmall.pms.entity.SpuDescEntity;
import com.atguigu.gmall.pms.service.SpuDescService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("spuDescService")
public class SpuDescServiceImpl extends ServiceImpl<SpuDescMapper, SpuDescEntity> implements SpuDescService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SpuDescEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SpuDescEntity>()
        );

        return new PageResultVo(page);
    }

//    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Transactional
    public void saveSpuDesc(SpuVo spuVo, Long spuVoId) {
        List<String> spuImages = spuVo.getSpuImages();
        //注意 由于yml文件中配置了数据库自增属性，所以必须要在vo类中注明不依靠数据库自动生成id,转为后端自主生成
        // 将图片集合转为字符串设置到decript介绍属性中
        if (!Collections.isEmpty(spuImages)) {
            //非空
            SpuDescEntity descEntity = new SpuDescEntity();
            descEntity.setDecript(StringUtils.join(spuImages, ","));
            descEntity.setSpuId(spuVoId);
            this.save(descEntity);
        }
    }

}